<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/20
 * Time: 下午8:28
 */
class StepService extends Service {

    public static function indexHeader($params){
        if($params['begin'] == $params['end']){
            $str = date('m.d', strtotime($params['begin']));
        }else{
            $str = date('m.d', strtotime($params['begin'])) . '~' . date('m.d', strtotime($params['end']));
        }

        return array(
            'Type'      => array('text' => 'Type', 'unSort' => true, 'width' => '100px'),
            'StepID'    => array('text' => 'Step ID', 'unSort' => true, 'width' => '100px'),
            'Cnt'       => array('text' => "数量 ({$str})", 'unSort' => true),
            'CumCnt'    => array('text' => '历史累计', 'unSort' => true),
        );
    }

    public static function indexData($params){
        $tmp = ModStep::getData($params);

        $result = array();
        foreach($tmp as $v){
            $result[$v['StepID']] = $v;
        }

        foreach($result as &$v){
            if($v['ParentID'] == 0){
                $v['PreCnt']    = isset($prev) ? $result[$prev]['Cnt'] : $v['Cnt'];
                $v['PreCumCnt'] = isset($prev) ? $result[$prev]['CumCnt'] : $v['CumCnt'];
                $v['RankLevel'] = 100000 * $v['StepID'];
                $prev = $v['StepID'];

                $v['Type'] = '主线步骤';
            }else{
                $v['RankLevel'] = 100000 * $v['ParentID'] + $v['StepID'];
                $v['PreCnt']    = $result[$v['ParentID']]['Cnt'];
                $v['PreCumCnt'] = $result[$v['ParentID']]['CumCnt'];

                $v['Type'] = "步骤{$v['ParentID']} - 支线";
            }
        }
        unset($v);

        $pubSort = new PubMultiSort('RankLevel');
        $result = $pubSort->getResult($result);

        $i = 1;
        foreach($result as &$v){
            if($i > 1){
                $v['Cnt'] .= ' <b style="color: green">('.number_format($v['Cnt'] / $v['PreCnt'] * 100, 2).'%)</b>';
                $v['CumCnt'] .= ' <b style="color: green">('.number_format($v['CumCnt'] / $v['PreCumCnt'] * 100, 2).'%)</b>';
            }
            $i++;
        }
        unset($v);
	if(!empty($result)) $result = array_values($result);
        return $result;
    }

}
